package edu.ucsd.cse110;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import edu.ucsd.cse110.model.Lift;
import edu.ucsd.cse110.model.Log;
import edu.ucsd.cse110.model.Wod;

/**
 * Displays a list of workout statistics.
 */
public class StatsActivity extends Activity implements OnClickListener {
	private String liftString = "";
	private Lift mLift;
	private Wod mWod;

	private final int REQUEST_CODE_WOD_DETAILS_SELECTION = 1;
	private final int REQUEST_CODE_LIFT_DETAILS_SELECTION = 2;
	private final int REQUEST_CODE_WOD_GRAPHS_SELECTION = 3;
	private final int REQUEST_CODE_LIFT_GRAPHS_SELECTION = 4;
	private String wodString = "";
	private List<Log> mLog;
	private List<Lift> mLiftLog;

	/**
	 * Displays a graph of body weight progression.
	 */
	public void bodyWeightGraphListener(View view) {
		BodyWeightGraph line = new BodyWeightGraph();

		Intent lineIntent = line.getIntent(this);

		if (lineIntent != null) {
			startActivity(lineIntent);
		} else {
			Toast.makeText(getBaseContext(),
					"There is no data for body weight!", Toast.LENGTH_SHORT)
					.show();
		}
	}

	/**
	 * TODO: Need documentation.
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode != RESULT_OK) {
			return;
		}
		LocalDatabaseManager database;

		switch (requestCode) {
		case REQUEST_CODE_WOD_DETAILS_SELECTION:
			database = new LocalDatabaseManager(this);
			mWod = database.getWodById(data.getExtras().getInt("Id")).get(0);
			mLog = database.getLogByWodId(data.getExtras().getInt("Id"));
			database.close();

			wodString = mWod.getName().toString();
			Bundle extras = new Bundle();
			extras.putInt("id", mWod.getId());
			extras.putString("wodString", wodString);
			if (mLog.size() > 0) {
				startActivity(new Intent(this, StatsWodDetailActivity.class)
						.putExtras(extras));
			} else {
				Toast.makeText(getBaseContext(),
						"There is no data for " + wodString + "!",
						Toast.LENGTH_SHORT).show();
			}
			break;
		case REQUEST_CODE_LIFT_DETAILS_SELECTION:
			database = new LocalDatabaseManager(this);
			mLift = database.getLiftByLiftId(data.getExtras().getInt("Id"))
					.get(0);
			mLiftLog = database.getLiftByLiftId(mLift.getLiftId());
			database.close();

			liftString = mLift.getName().toString();
			Bundle extras1 = new Bundle();
			extras1.putInt("id", data.getExtras().getInt("Id"));
			extras1.putString("liftString", liftString);
			if (mLiftLog.size() > 1) {
				startActivity(new Intent(this, StatsLiftDetailActivity.class)
						.putExtras(extras1));
			} else {
				Toast.makeText(getBaseContext(),
						"There is no data for this lift!", Toast.LENGTH_SHORT)
						.show();
			}
			break;
		case REQUEST_CODE_WOD_GRAPHS_SELECTION:
			database = new LocalDatabaseManager(this);
			mWod = database.getWodById(data.getExtras().getInt("Id")).get(0);
			database.close();

			wodString = mWod.getName().toString();
			StatsWodGraph line = new StatsWodGraph(wodString, data.getExtras()
					.getInt("Id"));
			Intent lineIntent = line.getIntent(this);

			if (lineIntent != null) {
				startActivity(lineIntent);
			} else {
				Toast.makeText(getBaseContext(),
						"There is no data for " + wodString + "!",
						Toast.LENGTH_SHORT).show();
			}
			break;
		case REQUEST_CODE_LIFT_GRAPHS_SELECTION:
			database = new LocalDatabaseManager(this);
			mLift = database.getLiftByLiftId(data.getExtras().getInt("Id"))
					.get(0);
			database.close();

			StatsLiftGraph line2 = new StatsLiftGraph(liftString, data
					.getExtras().getInt("Id"));
			Intent lineIntent2 = line2.getIntent(this);
			if (lineIntent2 != null) {
				startActivity(lineIntent2);
			}

			else {
				Toast.makeText(getBaseContext(),
						"There is no data for this lift!", Toast.LENGTH_SHORT)
						.show();
			}
			break;
		}
	}

	/**
	 * TODO: Need documentation.
	 */
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.stats_btn_woddetails:
			startActivityForResult(
					new Intent(this, WodSelectionActivity.class),
					REQUEST_CODE_WOD_DETAILS_SELECTION);
			break;
		case R.id.stats_btn_liftdetails:
			startActivityForResult(
					new Intent(this, LiftSelectionActivity.class),
					REQUEST_CODE_LIFT_DETAILS_SELECTION);
			break;
		case R.id.stats_btn_wodgraph:
			startActivityForResult(
					new Intent(this, WodSelectionActivity.class),
					REQUEST_CODE_WOD_GRAPHS_SELECTION);
			break;
		case R.id.stats_btn_liftgraph:
			startActivityForResult(
					new Intent(this, LiftSelectionActivity.class),
					REQUEST_CODE_LIFT_GRAPHS_SELECTION);
			break;
		}
	}

	/**
	 * TODO: Need documentation.
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.stats);

		Button btnWodDetails = (Button) findViewById(R.id.stats_btn_woddetails);
		btnWodDetails.setOnClickListener(this);

		Button btnLiftDetails = (Button) findViewById(R.id.stats_btn_liftdetails);
		btnLiftDetails.setOnClickListener(this);

		View btnWodGraphSelection = findViewById(R.id.stats_btn_wodgraph);
		btnWodGraphSelection.setOnClickListener(this);

		View btnOLGraphSelection = findViewById(R.id.stats_btn_liftgraph);
		btnOLGraphSelection.setOnClickListener(this);
	}
}